const {
  Maybe,
  Container,
  Left,
  Right,
  IO
} = require('./support');

const a = Container.of(5).map(x => x + 1).map(x => x * x);

// console.log(a)

const b = Maybe.of('hello world').map(x => null).map(x => x.toUpperCase());

// console.log(b)

function parseJSON(str) {
  try {
    return Right.of(JSON.parse(str));
  } catch ({
    message
  }) {
    return Left.of({
      error: message
    })
  }
}

const c = parseJSON('{"name":"ab"}')
  .map(x => x.name.toUpperCase());

// console.log(c)

const d = IO.of('abc');

// console.log(d._value());


const fs = require('fs');
const {
  task
} = require('folktale/concurrency/task');
const {
  split,
  find
} = require('lodash/fp');

// function readFile(filename) {
//   return task(resolver => {
//     fs.readFile(filename, 'utf-8', (err, data) => {
//       if (err) resolver.reject(err);

//       resolver.resolve(data);
//     })
//   })
// }

// readFile('package.json')
//   .map(split('\n'))
//   .map(find(x => x.includes('version')))
//   .run()
//   .listen({
//     onRejected: err => {
//       console.log(err)
//     },
//     onResolved: value => {
//       console.log(value)
//     }
//   })

const fp = require('lodash/fp')
let readFile = function (filename) {
  return new IO(function () {
    return fs.readFileSync(filename, 'utf-8')
  })
}

let print = function (x) {
  return new IO(function () {
    // console.log(x);
    return x;
  })
}

// let cat = fp.flowRight(print, readFile);

// let e= cat('package.json')._value()._value();
// console.log(e)

let f = readFile('package.json').map(fp.toUpper).flatMap(print).join();

console.log(f)